package com.visionork.android.twe.parser;

import java.net.URL;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.visionork.android.twe.activity.ShowSchedule;

public class VarietyShowScheduleAdapterImpl implements ShowScheduleAdapter {
	private static final String PREFIX = "http://sugoitw.sugoideas.com";
	private String _schedule_url = "";

	public VarietyShowScheduleAdapterImpl(String schedule_url) {
		_schedule_url = schedule_url;
	}

	@Override
	public ArrayList<ShowSchedule> get_schedule() throws Throwable {
		ArrayList<ShowSchedule> schedule_list = new ArrayList<ShowSchedule>();
		URL url = new URL(_schedule_url);
		Document doc = Jsoup.parse(url, 5000);
		Elements elms = doc.getElementsByTag("a");
		for (Element elm : elms) {
			String href = elm.attr("href");
			if (elm.hasAttr("target") && elm.attr("target").equals("_blank")
					&& elm.hasText() && href.startsWith(PREFIX)) {
				String title = elm.text();
				if (title != null && title.trim().length() > 0) {
					schedule_list.add(new ShowSchedule(title, href));
				}
			}
		}
		return schedule_list;
	}

}
